package com.dms.system.service.impl;

import java.util.List;

import com.dms.system.domain.SysMissionPlant;
import com.dms.system.domain.SysMissionUser;
import com.dms.system.mapper.SysMissionPlantMapper;
import com.dms.system.mapper.SysMissionUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dms.system.mapper.SysMissionMapper;
import com.dms.system.domain.SysMission;
import com.dms.system.service.ISysMissionService;
import com.dms.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务Service业务层处理
 * 
 * @author xlong_zhu
 * @date 2021-04-01
 */
@Service
public class SysMissionServiceImpl implements ISysMissionService 
{
    @Autowired
    private SysMissionMapper sysMissionMapper;

    @Autowired
    private SysMissionUserMapper sysMissionUserMapper;

    @Autowired
    private SysMissionPlantMapper sysMissionPlantMapper;

    /**
     * 查询任务
     * 
     * @param missionId 任务ID
     * @return 任务
     */
    @Override
    public SysMission selectSysMissionById(Long missionId)
    {
        return sysMissionMapper.selectSysMissionById(missionId);
    }

    @Override
    public List<Long> selectUserIdsByMissionId(Long missionId) {
        return sysMissionUserMapper.selectUserIdsByMissionId(missionId);
    }

    @Override
    public List<Long> selectPlantIdsByMissionId(Long missionId) {
        return sysMissionPlantMapper.selectPlantIdsByMissionId(missionId);
    }

    /**
     * 查询任务列表
     * 
     * @param sysMission 任务
     * @return 任务
     */
    @Override
    public List<SysMission> selectSysMissionList(SysMission sysMission)
    {
        return sysMissionMapper.selectSysMissionList(sysMission);
    }

    /**
     * 新增任务
     * 
     * @param sysMission 任务
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysMission(SysMission sysMission, SysMissionUser sysMissionUser, SysMissionPlant sysMissionPlant)
    {
        // 新增任务信息
        int rows = sysMissionMapper.insertSysMission(sysMission);
        Long missionId = sysMission.getMissionId();
        // 获取新增的任务编号
        sysMissionUser.setMissionId(missionId);
        sysMissionPlant.setMissionId(missionId);
        // 新增任务-员工关联
        sysMissionUserMapper.batchInsertMissionUser(sysMissionUser);
        // 新增任务-设备关联
        sysMissionPlantMapper.batchInsertMissionPlant(sysMissionPlant);
        return rows;
    }

    /**
     * 修改任务
     * 
     * @param sysMission 任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysMission(SysMission sysMission,SysMissionUser sysMissionUser,SysMissionPlant sysMissionPlant)
    {
        Long missionId = sysMission.getMissionId();
        // 删除mission与user关联
        sysMissionUserMapper.deleteMissionUser(missionId);
        // 删除mission与plant关联
        sysMissionPlantMapper.deleteMissionPlant(missionId);
        sysMissionUser.setMissionId(missionId);
        sysMissionPlant.setMissionId(missionId);
        // 新增mission与user关联
        sysMissionUserMapper.batchInsertMissionUser(sysMissionUser);
        // 新增mission与plant关联
        sysMissionPlantMapper.batchInsertMissionPlant(sysMissionPlant);
        return sysMissionMapper.updateSysMission(sysMission);
    }

    /**
     * 删除任务对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysMissionByIds(String ids)
    {
        String[] missionIds = Convert.toStrArray(ids);
        // 删除mission-user关联表中的记录
        sysMissionUserMapper.batchDeleteMissionUser(missionIds);
        // 删除mission-plant关联表中的记录
        sysMissionPlantMapper.batchDeleteMissionPlant(missionIds);
        return sysMissionMapper.deleteSysMissionByIds(missionIds);
    }

    /**
     * 删除任务信息
     * 
     * @param missionId 任务ID
     * @return 结果
     */
    @Override
    public int deleteSysMissionById(Long missionId)
    {
        return sysMissionMapper.deleteSysMissionById(missionId);
    }
}
